﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Wicresoft.SMS.Data
{
    public class sqlWebAddressDataAccess : IAddressWebDataAccess
    {
        private IConnectStringProvider _connParamProvider;
         private string _connectionString;

         public sqlWebAddressDataAccess(string ConnectionString)
        {
            _connectionString = ConnectionString;
        }

         public List<AddressSendInfoModel> GetNumberByUser(int UserId, int index, int max, string key)
         {
             using (var dbModel = new Entities(_connectionString))
             {
                 var s = (from n in dbModel.Number
                         join c in dbModel.Contact
                         on n.AddressID equals c.ID
                         where c.UserID == UserId
                         orderby c.ID
                         select new
                         {
                             number = c.DisplayName,
                             name = n.Number1,
                             addressId = n.AddressID
                         }).Skip((index - 1) * max).Take(max);
                 List<AddressSendInfoModel> list = new List<AddressSendInfoModel>();
                 foreach (var n in s)
                 {
                     AddressSendInfoModel _list = new AddressSendInfoModel();
                     _list.Name = n.name;
                     _list.Number = n.number;
                     _list.AddressId = n.addressId;
                     list.Add(_list);
                 }
                 return list;
             }
             throw new NotImplementedException();
         }

         public int AddAddress(string name, string number)
         {
             //TODO得到当前用户
             int userId = 2;
             using (var dbModel = new Entities(_connectionString))
             {
                 Contact c = new Contact();
                 c.UserID = userId;
                 c.DisplayName = name;
                 c.Enabled = true;
                 dbModel.AddToContact(c);
                 dbModel.SaveChanges();
                 //根据刚刚插入的Contact表的ID，再添加到Number表
                 Number n = new Number();
                 n.Number1 = number;
                 n.AddressID = c.ID;
                 dbModel.AddToNumber(n);
                 dbModel.SaveChanges();
             }
             throw new NotImplementedException();
         }


         public List<AddressSendInfoModel> GetNumberByText(string text,int UserId)
         {
             using (var dbModel = new Entities(_connectionString))
             {
                 var s = from n in dbModel.Number
                         join c in dbModel.Contact
                         on n.AddressID equals c.ID
                         where c.DisplayName.Contains(text) || n.Number1.Contains(text)
                         where c.UserID == UserId
                         select new
                         {
                             number = c.DisplayName,
                             name = n.Number1
                         };
                 List<AddressSendInfoModel> list = new List<AddressSendInfoModel>();
                 foreach (var n in s)
                 {
                     AddressSendInfoModel _list = new AddressSendInfoModel();
                     _list.Name = n.name;
                     _list.Number = n.number;
                     list.Add(_list);
                 }
                 return list;
             }
             throw new NotImplementedException();
         }

           public bool DeleteMessageById(int Id)
        {
            using (var dbModel = new Entities(_connectionString))
            {
                var info = dbModel.Message.SingleOrDefault(e => e.ID == Id);
                dbModel.Message.DeleteObject(info);
                try
                {
                    dbModel.SaveChanges();
                }
                catch
                {
                    return false;
                }
                return true;
            }
            throw new NotImplementedException();
        }

           public bool DeleteNumberByAddressId(int addressId)
           {
               using (var dbModel = new Entities(_connectionString))
               {
                   var info = dbModel.Number.FirstOrDefault(n => n.AddressID == addressId );
                   dbModel.Number.DeleteObject(info);
                   try
                   {
                       dbModel.SaveChanges();
                   }
                   catch
                   {
                       return false;
                   }
                   return true;
               }
               throw new NotImplementedException();
           }
           public int GetPageCount(int UserId, int max, string key)
           {
               using (var dbModel = new Entities(_connectionString))
               {
                   var s = from n in dbModel.Number
                           join c in dbModel.Contact
                           on n.AddressID equals c.ID
                           where c.DisplayName.Contains(key) || n.Number1.Contains(key)
                           where c.UserID == UserId
                           select new
                           {
                               number = c.DisplayName,
                               name = n.Number1
                           };
                   int count = (s.Count() + (max - 1)) / max;
                   return count;
               }
           }


           public List<AddressSendInfoModel> GetNumberByUsers(int UserId)
           {
               using (var dbModel = new Entities(_connectionString))
               {
                   var s = from n in dbModel.Number
                           join c in dbModel.Contact
                           on n.AddressID equals c.ID
                           where c.UserID == UserId
                           orderby c.ID
                           select new
                           {
                               number = c.DisplayName,
                               name = n.Number1,
                               addressId = n.AddressID
                           };
                   List<AddressSendInfoModel> list = new List<AddressSendInfoModel>();
                   foreach (var n in s)
                   {
                       AddressSendInfoModel _list = new AddressSendInfoModel();
                       _list.Name = n.name;
                       _list.Number = n.number;
                       _list.AddressId = n.addressId;
                       list.Add(_list);
                   }
                   return list;
               }
               throw new NotImplementedException();
           }
    }
}
